$(function(){
    PLATFORM.timepickerRange("#timepickerStart", "#timepickerEnd");
    
    var opts_coupon = {
            targetId: "couponTb",
            templateId: "couponTbTemplate",
            settings: {
                url: DI.queryHorizActivity,
                ajaxType: "json",
                ajaxDataHandler: function(data){
                    var temp = {};
                    temp.total = data.data.totalSize;
                    if(data.data){
                        temp.rows = data.data.records;
                    }else{
                        temp.rows = [];
                        throw "数据结构错误，缺少rows参数，请检查返回值";
                    }
                    
                    return temp;
                },
                paginationIndexName:{
                    "peerpageRows":'pageSize',
                    "currentPage":'pageNo'
                },
            },
            customFuncs:{
                operate: function(el){
                    couponCtrl.isUsed = el.isUsed;
                    couponCtrl.proActivityId = el.proActivityId;
                    couponCtrl.popup.operate = true;
                },
                create: function(el){
                    couponCtrl.popup.loading = true;
                    couponCtrl.popup.loadingTxt = "正在生成";
                    $.ajax({
                        type: "post",
                        url: DI.batchCreateDiffIndustCert,
                        data: {
                            activityId: el.proActivityId,
                            count: el.remainActCount
                        },
                        dataType: "json",
                        success: function(data){
                            if(data.retFlag == 1){
                                PLATFORM.headTip("操作成功", "#53CC2D");
                                couponTable.refresh();
                            }else{
                                PLATFORM.headTip(data.errorMessage);
                            }
                            couponCtrl.popup.loading = false;
                        }
                    });
                },
                exportData: function(el){
                    couponCtrl.popup.export = true;
                    couponCtrl.dValue = el.dValue;
                    couponCtrl.remainActCount = el.remainActCount;
                    couponCtrl.activityName = el.activityName;
                    couponCtrl.activitySecretKey = el.activitySecretKey;
                    couponCtrl.endTime = el.endTime;
                    couponCtrl.proActivityId = el.proActivityId;
                    couponCtrl.proActivityName = el.proActivityName;
                    couponCtrl.startTime = el.startTime;
                }, 
                exportHistory: function(el){
                    couponCtrl.historyShow = true;
                    couponCtrl.actCount = el.actCount;
                    couponCtrl.dValue = el.dValue;
                    couponCtrl.hasSend = el.hasSend;
                    couponCtrl.activityName = el.activityName;
                    couponCtrl.activitySecretKey = el.activitySecretKey;
                    couponCtrl.endTime = el.endTime;
                    couponCtrl.proActivityId = el.proActivityId;
                    couponCtrl.proActivityName = el.proActivityName;
                    couponCtrl.startTime = el.startTime;
                    couponCtrl.remainActCount = el.remainActCount;
                    opts_history.settings.searchParam.activityId = el.proActivityId;
                    //如果没初始化就初始化 初始化了就刷新
                    if(historyTable){                                                                
                        historyTable.refresh();
                    }else{
                        historyTable = SwiftUI.swiftTable.getInstance(opts_history);
                    }
                }
            }
        },
        opts_history = {
            targetId: "historyTb",
            templateId: "historyTbTemplate",
            settings: {
                url: DI.queryHorizProRecords,
                ajaxType: "json",
                searchParam: {activityId: ""},
                ajaxDataHandler: function(data){
                    var temp = {};
                    temp.total = data.data.totalSize;
                    if(data.data){
                        temp.rows = data.data.records;
                    }else{
                        temp.rows = [];
                        throw "数据结构错误，缺少rows参数，请检查返回值";
                    }
                    
                    return temp;
                },
                paginationIndexName:{
                    "peerpageRows":'pageSize',
                    "currentPage":'pageNo'
                },
            },
            customFuncs:{
                del: function(el){
                    couponCtrl.popup.del = true;
                    couponCtrl.horizProCoupon.importBatch = el.importBatch;
                    couponCtrl.horizProCoupon.importNum = el.importNum;
                }
            }
        },
        couponTable = SwiftUI.swiftTable.getInstance(opts_coupon),
        historyTable = null;
    
    var couponCtrl = avalon.define({
            $id: "coupon",
            sActivityName: "",
            sStartTime: "",
            sEndTime: "",
            actCount: 0,
            dValue: 0,
            hasSend: 0,
            activityName: "",
            activitySecretKey: "",
            remainActCount: 0,
            endTime: "",
            proActivityId: "",
            proActivityName: "",
            startTime: "",
            isUsed: "0",
            horizProCoupon: {
                importBatch: "",
                importNum: 0
            },
            historyShow: false,
            popup: {
                export: false,
                exportSure: function(){
                    couponCtrl.popup.loading = true;
                    couponCtrl.popup.loadingTxt = "正在导入";
                $("#form_uploadFile").ajaxSubmit({
                    type: "post",
                    url: DI.importPro,
                    success: function(data){
                            couponCtrl.popup.loading = false;
                        if(data.retFlag == 1){
                            PLATFORM.headTip("操作成功", "#53CC2D");
                                couponCtrl.popup.export = false;
                            couponTable.refresh();
                            if(historyTable){
                            historyTable.refresh();
                            }
                            couponCtrl.actCount += couponCtrl.horizProCoupon.importNum;
                            couponCtrl.dValue -= couponCtrl.horizProCoupon.importNum;
                        }else{
                            PLATFORM.headTip(data.errorMessage);
                        }
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown){
                        console.log("ajax error");
                        console.log(XMLHttpRequest);
                    }
                });
            },
                operate: false,
                operateSure: function(){
                    var optCode = couponCtrl.isUsed == "0" ? "1" : "0";
                    
                    couponCtrl.popup.operate = false;
                    $.ajax({
                        type: "post",
                        url: DI.updateActivityStatus,
                        data: {
                            activityId: couponCtrl.proActivityId,
                            optCode: optCode
                        },
                        dataType: "json",
                        success: function(data){
                            if(data.retFlag == 1){
                                couponTable.refresh();
                                PLATFORM.headTip("操作成功", "#53CC2D");
                            }else{
                                PLATFORM.headTip(data.errorMessage);
                            }
                        }
                });
            },
                del: false,
            delSure: function(){
                    couponCtrl.popup.del = false;
                $.ajax({
                    type: "post",
                    url: DI.deleteHorizProCoupon,
                    data: {
                        activityId: couponCtrl.proActivityId,
                        importBatch: couponCtrl.horizProCoupon.importBatch
                    },
                    dataType: "json",
                    success: function(data){
                        if(data.retFlag == 1){
                            PLATFORM.headTip("操作成功", "#53CC2D");
                            couponTable.refresh();
                            historyTable.refresh();
                            couponCtrl.actCount -= couponCtrl.horizProCoupon.importNum;
                            couponCtrl.dValue += couponCtrl.horizProCoupon.importNum;
                        }else{
                            PLATFORM.headTip(data.errorMessage);
                        }
                    }
                });
            },
                loading: false,
                loadingTxt: "",
                close: function(){
                    couponCtrl.popup.export = couponCtrl.popup.operate = couponCtrl.popup.del = false;
            }
            },
            exportData: function(){
                couponCtrl.popup.export = true;
            }, 
            back: function(){
                couponCtrl.historyShow = false;
            },
            search: function(){
                couponTable.search({
                   activityName: couponCtrl.sActivityName,
                   startTime: couponCtrl.sStartTime,
                   endTime: couponCtrl.sEndTime
                });
            },
        });
    avalon.scan();
});

avalon.filters.isUsed = function(isUsed){
    return isUsed == 0 ? "未启用" : "启用";
};
avalon.filters.isUsedBtn = function(isUsed){
    return isUsed == 0 ? "启用" : "停用";
};


